package com.bci.pwtz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ProductionInfoMapper;
import com.bci.pwtz.mysql.model.ProductionInfo;
import com.bci.pwtz.service.ProductionInfoService;

public class ProductionInfoServiceImpl implements ProductionInfoService{
private ProductionInfoMapper objMapper;
private static final Logger _logger=Logger.getLogger(ProductionInfoServiceImpl.class);
	public ProductionInfoMapper getObjMapper() {
	return objMapper;
}

public void setObjMapper(ProductionInfoMapper objMapper) {
	this.objMapper = objMapper;
}

	@Override
	public int insert(ProductionInfo pro) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("production:  insert="+pro.getId());
		}
		int result =  0;
		try {
			String desc = pro.getUpdatedesc();
			desc=desc.replaceAll("\\|", "<br>");
			pro.setUpdatedesc(desc);
			objMapper.insert(pro);
			result  = 1;
		}catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public int deleleById(int id) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("production:  deletebyid="+id);
		}
		int result = 0;
		try {
			objMapper.deleleById(id);
			result = 1;
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	}

	@Override
	public int update(ProductionInfo pro) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("production:  update="+pro.getId());
		}
		int result = 0;
		try {
			String desc = pro.getUpdatedesc();
			desc=desc.replaceAll("\\|", "<br>");
			pro.setUpdatedesc(desc);
			objMapper.update(pro);
			result = 1;
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return result;
	
	}

	@Override
	public ProductionInfo load(int id) throws PwtzDatabaseException {
		if(_logger.isDebugEnabled()){
			_logger.debug("production:  load="+id);
		}
		ProductionInfo info =null;
		try {
			info = objMapper.load(id);
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return info;
	}

	@Override
	public List<ProductionInfo> getProList(int type) throws PwtzDatabaseException{
		if(_logger.isDebugEnabled()){
			_logger.debug("production:  getlist:type="+type);
		}
		List<ProductionInfo> list=new ArrayList<ProductionInfo>();
		try {
			list= objMapper.getProList(type);
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		return list;
	}

}
